package com.ordertracker.service;

import com.ordertracker.Application;
import com.ordertracker.Session;
import com.ordertracker.dao.GeneralCrudDao;
import com.ordertracker.persistent.dictionary.AuditEntity;
import com.ordertracker.persistent.dictionary.Organization;
import com.ordertracker.protocol.CommandMessage;
import com.ordertracker.to.TransferObjectFactory;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBException;
import java.io.StringReader;

/**
 * Description of class.
 * Created 1/15/13 11:43 AM
 *
 * @author andrey.rodin@playtech.com
 */

@Service(value = "organization")
public class OrganizationManager implements HandleChain{

    private static Logger logger = Logger.getLogger(OrganizationManager.class);

    private HandleChain chain;

    private GeneralCrudDao<Organization, Long> dao;

    private TransferObjectFactory transferObjectFactory;

    @Resource(name = "defaultMan")
    public void setChain(HandleChain chain) {
        this.chain = chain;
    }

    @Resource(name = "crudDao")
    public void setDao(GeneralCrudDao<Organization, Long> dao) {
        this.dao = dao;
    }

    @Resource(name = "tof")
    public void setTransferObjectFactory(TransferObjectFactory transferObjectFactory) {
        this.transferObjectFactory = transferObjectFactory;
    }

    @Override
    public Response handleMessage(Session session, CommandMessage msg, ActionType type) throws WebApplicationException {
        if (! msg.getType().toLowerCase().contains("organization")) {
            chain.handleMessage(session, msg, type);
        }

        switch (type) {
          case ADD:
              return Response.ok(createOrganization(session, msg)).build();
          case GET:
              return Response.ok(getOrganization(msg)).build();
        }

        return Response.ok().build();
    }

    @Transactional(readOnly = true)
    private com.ordertracker.entity.Organization getOrganization(CommandMessage msg) {
        Long id = Long.valueOf(msg.getObj());

        return transferObjectFactory.organizationToDTO(dao.findById(id, Organization.class));
    }

    @Transactional
    private com.ordertracker.entity.Organization createOrganization(Session session, CommandMessage msg) {
        com.ordertracker.entity.Organization organization = null;
        try {
            organization = Application.getApplication().getJsonUnmarshaller().unmarshalFromJSON(new StringReader(msg.getObj()), com.ordertracker.entity.Organization.class);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        AuditEntity auditEntity = transferObjectFactory.generateAuditInfo(session, false);
        Organization orgPersist = dao.saveEntity(transferObjectFactory.organizationFromDTO(organization, auditEntity));
        organization.setId(orgPersist.getId());
        organization.setVersion(orgPersist.getVersion());
        return organization;
    }
}
